using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;

namespace Baz
{
    public class Collision
    {
        public static bool Intersects(Rectangle a, Rectangle b)
        {
            return (a.Right > b.Left && a.Left < b.Right &&
                a.Bottom > b.Top && a.Top < b.Bottom);
        }

        public static bool Intersects(Sprite a, Sprite b)
        {
            if (Collision.Intersects(a.boundBox, b.boundBox))
            {
                uint[] bitsA = new uint[a.Texture.Width * a.Texture.Height];
                a.Texture.GetData<uint>(bitsA);

                uint[] bitsB = new uint[b.Texture.Width * b.Texture.Height];
                b.Texture.GetData<uint>(bitsB);

                int x1 = Math.Max(a.boundBox.X, b.boundBox.X);
                int x2 = Math.Min(a.boundBox.X + a.boundBox.Width, b.boundBox.X + b.boundBox.Width);

                int y1 = Math.Max(a.boundBox.Y, b.boundBox.Y);
                int y2 = Math.Min(a.boundBox.Y + a.boundBox.Height, b.boundBox.Y + b.boundBox.Height);

                for (int y = y1; y < y2; ++y)
                {
                    for (int x = x1; x < x2; ++x)
                    {
                        if (((bitsA[(x - a.boundBox.X) + (y - a.boundBox.Y) * a.Texture.Width] & 0xFF000000)>>24) > 20 &&
                            ((bitsB[(x - b.boundBox.X) + (y - b.boundBox.Y) * b.Texture.Width] & 0xFF000000)>>24) > 20)
                            return true;
                    }
                }
                return false;
            }
            return false;
        }
    }
}
